package com.china.school.autoproxycreator;

import com.china.school.pojo.User;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

public class DefaultAdvisorAutoProxyCreatorTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext contxt = new AnnotationConfigApplicationContext(DefaultAdvisorAutoProxyCreatorTest.class);
        User user = contxt.getBean(User.class);
        user.eat();


    }

    @Bean
    public DefaultPointcutAdvisor defaultPointcutAdvisor(){
        DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();
        NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
        pointcut.addMethodName("eat");
        defaultPointcutAdvisor.setPointcut(pointcut);
        defaultPointcutAdvisor.setAdvice(new MethodInterceptor() {
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                System.out.println("--------around----前");
                invocation.proceed();
                System.out.println("--------around----后");
                return null;
            }
        });

        return defaultPointcutAdvisor;
    }


    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        autoProxyCreator.setProxyTargetClass(true);
         return autoProxyCreator;

    }


}
